package com.gz.moreaseManager.modules.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gz.moreaseManager.core.base.BaseController;
import com.gz.moreaseManager.core.modal.AjaxResult;
import com.gz.moreaseManager.core.modal.HttpCode;
import com.gz.moreaseManager.core.modal.PageResult;
import com.gz.moreaseManager.modules.entity.AssistInfo;
import com.gz.moreaseManager.modules.service.IAssistInfoService;
import com.gz.moreaseManager.utils.DateUtil;
import com.gz.moreaseManager.utils.UtilTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 帮助信息 前端控制器
 * </p>
 *
 * @author wike
 * @since 2019-05-28
 */
@RestController
@RequestMapping("/assistInfo")
public class AssistInfoController extends BaseController {

    @Autowired
    private IAssistInfoService iAssistInfoService;

    @RequestMapping("list")
    public ModelAndView list(){
        return new ModelAndView("modules/assistInfo/list");
    }

    @GetMapping("add")
    public ModelAndView add(){
        return new ModelAndView("modules/assistInfo/add");
    }

    @GetMapping("update")
    public ModelAndView update(Integer id){
        AssistInfo assistInfo = iAssistInfoService.selectById( id );
        ModelAndView module = new ModelAndView("modules/assistInfo/update");
        module.addObject("assistInfo",assistInfo);
        return module;
    }

    @GetMapping("detail")
    public ModelAndView detail(Integer id){
        AssistInfo assistInfo = iAssistInfoService.queryAssistDetail( id );
        ModelAndView module = new ModelAndView("modules/assistInfo/detail");
        module.addObject("assistInfo",assistInfo);
        return module;
    }

    @GetMapping("listPage")
    public PageResult<AssistInfo> findPageLsit(String name, Integer type,
                                            @DateTimeFormat(pattern = "yyyy-MM-dd")Date startDate,
                                            @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                                            Integer pageNum, Integer pageSize){
        //分页
        Page paramsPage = new Page();
        paramsPage.setCurrent(pageNum);
        paramsPage.setSize(pageSize);
        //构建查询
        Wrapper wrapper = new EntityWrapper<AssistInfo>();
        if( UtilTool.isNotBlank( name ))
            wrapper.like("assist_title",name);
        if( UtilTool.isNotBlank( type ))
            wrapper.eq("help_id",type);
        if( UtilTool.isNotBlank( startDate ) ){
            if( UtilTool.isNotBlank( endDate ) && startDate.getTime() == endDate.getTime() ){
                wrapper.between("create_time",startDate,DateUtil.addDays(startDate,1));
            }else{
                wrapper.between("create_time",startDate,
                        UtilTool.isNotBlank(endDate)?DateUtil.addDays(endDate,1):DateUtil.addDays(new Date(),1));
            }
        }

        if( UtilTool.isBlank(startDate) && UtilTool.isNotBlank( endDate )){
            wrapper.le("create_time",DateUtil.addDays(endDate,1));
        }
        //排序
        wrapper.orderBy("create_time",false);
        Page<AssistInfo> data = iAssistInfoService.selectPage(paramsPage,wrapper);
        return new PageResult(0, null, data.getTotal(), data.getRecords());

    }

    /**
     * 新增帮助
     * @param assistInfo
     * @return
     */
    @PostMapping("addAssistInfo")
    public AjaxResult<Boolean> addAssistInfo(@RequestBody AssistInfo assistInfo){
        if( assistInfo == null
                || UtilTool.isBlank( assistInfo.getAssistTitle())
                || UtilTool.isBlank( assistInfo.getHelpId())
                || UtilTool.isBlank( assistInfo.getAssistText()))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        assistInfo.setCreateTime(new Date());
        Boolean success = iAssistInfoService.insert(assistInfo);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "新增帮助信息失败!");
    }

    /**
     * 获取帮助信息
     * @param id
     * @return
     */
    @GetMapping("findAssistInfo")
    public AjaxResult<AssistInfo> findAssistInfo( Integer id ){
        if( UtilTool.isBlank( id ))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        AssistInfo assistInfo = iAssistInfoService.selectById( id );
        return AjaxResult.success(assistInfo);

    }

    /**
     * 修改帮助
     * @param assistInfo
     * @return
     */
    @PostMapping("editAssistInfo")
    public AjaxResult<Boolean> editAssistInfo(@RequestBody AssistInfo assistInfo){
        if( assistInfo == null
                || UtilTool.isBlank( assistInfo.getAssistId())
                || UtilTool.isBlank( assistInfo.getAssistTitle())
                || UtilTool.isBlank( assistInfo.getHelpId())
                || UtilTool.isBlank( assistInfo.getAssistText()))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        Boolean success = iAssistInfoService.updateById(assistInfo);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "修改帮助信息失败!");
    }

    /**
     * 根据ID删除帮助信息
     * @param id
     * @return
     */
    @PostMapping("delete")
    public AjaxResult<Boolean> delete( Integer id){
        if( UtilTool.isBlank( id ))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        Boolean success = iAssistInfoService.deleteById( id );
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "删除帮助信息失败!");
    }

    /**
     * 根据帮助分类ID查询帮组信息
     * @param helpId
     * @return
     */
    @GetMapping("findAssistListById")
    public AjaxResult<List<AssistInfo>> findAssistListByHelpId(Integer helpId ){
        if( UtilTool.isBlank( helpId ))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        Wrapper wrapper = new EntityWrapper<AssistInfo>();
        wrapper.eq("help_id",helpId);
        List<AssistInfo> list = iAssistInfoService.selectList(wrapper);
        if( UtilTool.isNotBlank( list ))
            return AjaxResult.success(list );

        return AjaxResult.build(HttpCode.FAILED, "查询帮助信息失败!");
    }
}
